package com.huifaapp.mall.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.FutureTarget;
import com.google.gson.Gson;
import com.huifaapp.mall.APP;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.Homefragment.ChioceActivity;
import com.huifaapp.mall.adapter.Tmall_homepageAdapter;
import com.huifaapp.mall.adapter.Tmall_homepage_cehuaAdapter;
import com.huifaapp.mall.adapter.TmallchoicenessAdapter;
import com.huifaapp.mall.adapter.TmallchoicenessListAdapter;
import com.huifaapp.mall.adapter.Tnall_specialAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.api.ObserverCallback;
import com.huifaapp.mall.bean.JDcomAdvertisingBean;
import com.huifaapp.mall.bean.JDcomBannerbean;
import com.huifaapp.mall.bean.JDcomHotlistBean;
import com.huifaapp.mall.bean.JDcomOEMBeam;
import com.huifaapp.mall.bean.JDcomclassifyBean;
import com.huifaapp.mall.bean.JDcomjPlistBean;
import com.huifaapp.mall.bean.JDlianjie;
import com.huifaapp.mall.bean.ShareImageBean;
import com.huifaapp.mall.bean.SpecialGddodsBean;
import com.huifaapp.mall.dialogs.LoadingDialog;
import com.huifaapp.mall.fragment.TmallClassifyBaseFragment;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.Contig;
import com.huifaapp.mall.uitils.GlideImageLoader;
import com.huifaapp.mall.uitils.ShareUtils;
import com.huifaapp.mall.uitils.UtilsImage;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.kepler.jd.Listener.LoginListener;
import com.kepler.jd.login.KeplerApiManager;
import com.kepler.jd.sdk.exception.KeplerBufferOverflowException;
import com.squareup.okhttp.Request;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static com.huifaapp.mall.afinal.Constants.savePath;


/**
 * 天猫页面
 */


    public class TmallhomepageFragment extends TmallClassifyBaseFragment{
    private XRecyclerView mRecyclerview;
    //获取数据的开始
    private int curr=0;
    private ImageView floatingActionButton;
    public boolean isSelected = true;
    private int lastPosition;
    private SharedPreferences sharedPreferences;
    private int lastOffset;
    public static Map<Integer, Boolean> hmap;
    private ImageView dialog_tmall_floatingActionButton_share;
    private TmallchoicenessAdapter myRecycleViewAdapter;
    private String BANNERSURL = ApiManager.BASE_NIUPAI_URL+Contig.typex+"/banners";//banners轮播图
    private  String MODELSURL=ApiManager.BASE_NIUPAI_URL+Contig.typex+"/models";//八大专区分类
    private  String  ADVERTSURL=ApiManager.BASE_NIUPAI_URL+Contig.typex+"/adverts";//广告位
    private  String  GOODSURL=ApiManager.BASE_NIUPAI_URL+Contig.typex+"/goods";//，精选，爆品二合一接口
    private  String SYNCJDSPREADURL=ApiManager.BASE_NIUPAI_URL+Contig.typex+"/getUrl";//京东普通二合一链接
    private  String GETCONURLURL=ApiManager.BASE_NIUPAI_URL+Contig.typex+"/getConUrl";//京东获取优惠劵二合一链接
    private  String SPECIALLISTURL=ApiManager.BASE_NIUPAI_URL+Contig.typex+"special/speciallist";//特卖接口
    private RecyclerView cus;
    private XRecyclerView list_xrecyclerView;
    private View banner_frg01;
    private View mview;
    private TmallchoicenessListAdapter tmallchoicenessListAdapter;
    private ImageView tmall_popupWindow;
    private PopupWindow mPopupMenu;
    private List<JDcomBannerbean.DataBean> bannerlist;
    private Banner tmall_banner;
    private List<String> bannerimge;
    private List<JDcomOEMBeam.DataBean> oemlist;
    private List<String> oeMimgeUrllist;
    private List<JDcomAdvertisingBean.DataBean> jDcomAdvertisinglist;
    private ImageView jDcomAdvertising_imge;
    private String jDcomAdvertisingimge;
    private RelativeLayout tmall_xiayichang;
    private List<JDcomjPlistBean.DataBean.ListBean> jDcomjPlistlist;
    private List<JDcomclassifyBean.DataBean> classif;
//    private RecyclerView tmall_recyclerView;
    private SpecialGddodsBean specialGddodsBean;
    private RecyclerView tmall_seckill_recyclerView;
    private LinearLayout tmall_activity_quanbu;
    private List<JDcomHotlistBean.DataBean.ListBean> jDcomHotlist;
    private SpecialGddodsBean.DataBean.ListBean dsdlistBean;
    private int location;
    private String datalisnajie;
    private JDlianjie jDlianjiebean;
    private String datayouhui;
    private static final int SHARET_WXHY = 517;
    ArrayList<String> idlist = new ArrayList<>();
    ArrayList<String> idlistlv = new ArrayList<>();
    private int lvgv=0;
    private TextView more;
    private String jDcomAdvertisingID;
    private LinearLayoutManager recylerViewLayoutManager;
    private LinearLayout ll_activity_adv;
    private ImageView iv_adv1;
    private ImageView iv_adv2;
    private ImageView iv_adv3;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        try {
            if (mview != null) {
                ViewGroup parent = (ViewGroup) mview.getParent();
                if (null != parent) {
                    parent.removeView(mview);
                }
            } else {
                mview = inflater.inflate(R.layout.dialog_tmall, container, false);
                initView();
                initbannerHttp();
                initOEMHttp();
                initAdvertisingHttp();
                initSpeciallistHttp();
                getData();
                hmap = new HashMap<>();
                }


        }catch (Exception e){
            Log.e("Exception",e.getMessage());
        }
        return mview;
    }

    @Override
    public void onStart() {
        super.onStart();
//        jDcomjPlistlist.clear();
//        initjplistHttp();
    }




//    @Override
//    public void onResume() {
//        super.onResume();
////        jDcomjPlistlist.clear();
//        initjplistHttp();
//    }





    //    @Override
//    public void onResume() {
//        super.onResume();
//        // scrollToPosition();
//
//        if (recylerViewLayoutManager == null) {
//            recylerViewLayoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);
//        }
//        mRecyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false));
//
//        initjplistHttp();
////        myRecycleViewAdapter = new TmallchoicenessAdapter(jDcomjPlistlist, getActivity(), "");
////        mRecyclerview.setAdapter(myRecycleViewAdapter);
//    }


    private void initView() {
        try {
            initjplistHttp();
            jDcomjPlistlist = new ArrayList<JDcomjPlistBean.DataBean.ListBean>();
            mRecyclerview = (XRecyclerView) mview.findViewById(R.id.SY_xrecyclerView);
            myRecycleViewAdapter = new TmallchoicenessAdapter(jDcomjPlistlist, getActivity(), "");
            mRecyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false));
            mRecyclerview.setAdapter(myRecycleViewAdapter);
            mRecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
                @Override
                public void onRefresh() {
                    curr = 0;
                    initjplistHttp();
                }

                @Override
                public void onLoadMore() {
                    curr++;
                    initjplistHttp();
                }
            });

            list_xrecyclerView = (XRecyclerView) mview.findViewById(R.id.list_xrecyclerView);
            tmallchoicenessListAdapter = new TmallchoicenessListAdapter(getActivity(), jDcomjPlistlist, "");
            list_xrecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
            list_xrecyclerView.setAdapter(tmallchoicenessListAdapter);
            list_xrecyclerView.setVisibility(View.GONE);
            list_xrecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
                @Override
                public void onRefresh() {
                    curr = 0;
                    initjplistHttp();
                }

                @Override
                public void onLoadMore() {
                    curr++;
                    initjplistHttp();
                }
            });
            tmall_popupWindow = (ImageView) mview.findViewById(R.id.tmall_PopupWindow);
            banner_frg01 = LayoutInflater.from(getActivity()).inflate(R.layout.activity_tmall, null);
            mRecyclerview.addHeaderView(banner_frg01);
            list_xrecyclerView.addHeaderView(banner_frg01);
//        floatingActionButton = (FloatingActionButton) mview.findViewById(R.id.dialog_tmall_floatingActionButton);
            floatingActionButton = (ImageView) mview.findViewById(R.id.dialog_tmall_floatingActionButton);
//        dialog_tmall_floatingActionButton_share = (FloatingActionButton) mview.findViewById(R.id.dialog_tmall_floatingActionButton_share);
            dialog_tmall_floatingActionButton_share = (ImageView) mview.findViewById(R.id.dialog_tmall_floatingActionButton_share);
//        floatingActionButton = (FloatingActionButton) mview.findViewById(R.id.dialog_tmall_floatingActionButton);
            Log.e("判断用户身份", SPUtils.getInstance().getString(SPConstants.KEY_ISSTORE));
            if (SPUtils.getInstance().getString(SPConstants.KEY_ISSTORE).equals("1")) {
                dialog_tmall_floatingActionButton_share.setVisibility(View.VISIBLE);
            } else {
                dialog_tmall_floatingActionButton_share.setVisibility(View.GONE);
            }

            cus = (RecyclerView) banner_frg01.findViewById(R.id.cus);
            tmall_banner = (Banner) banner_frg01.findViewById(R.id.tmall_banner);
            more = (TextView) banner_frg01.findViewById(R.id.more);
            more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), ChioceActivity.class);
                    startActivity(intent);
                }
            });
            tmall_activity_quanbu = (LinearLayout) banner_frg01.findViewById(R.id.tmall_activity_quanbu);
            tmall_xiayichang = (RelativeLayout) banner_frg01.findViewById(R.id.tmall_xiayichang);
            ll_activity_adv = (LinearLayout) banner_frg01.findViewById(R.id.ll_activity_adv);
            iv_adv1 = (ImageView) banner_frg01.findViewById(R.id.iv_adv1);
            iv_adv2 = (ImageView) banner_frg01.findViewById(R.id.iv_adv2);
            iv_adv3 = (ImageView) banner_frg01.findViewById(R.id.iv_adv3);

            if(Contig.typex.equals("jd")){
                ll_activity_adv.setVisibility(View.VISIBLE);
            }else{
                ll_activity_adv.setVisibility(View.GONE);
            }

//          tmall_recyclerView = (RecyclerView) banner_frg01.findViewById(R.id.tmall_RecyclerView);
            jDcomAdvertising_imge = (ImageView) banner_frg01.findViewById(R.id.JDcomAdvertising_imge);
            tmall_seckill_recyclerView = (RecyclerView) banner_frg01.findViewById(R.id.tmall_seckill_RecyclerView);
            tmall_xiayichang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent Intent = new Intent(getActivity(), MinuteKillActivity.class);
                    Intent.putExtra("type", Contig.typex);
                    startActivity(Intent);
                }
            });
            tmall_activity_quanbu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent Intent = new Intent(getActivity(), MinuteKillActivity.class);
                    Intent.putExtra("type", Contig.typex);
                    startActivity(Intent);
                }
            });
            floatingActionButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isSelected) {
                        isSelected = false;
                        mRecyclerview.setVisibility(View.GONE);
                        list_xrecyclerView.setVisibility(View.VISIBLE);
                        //scrollToPosition();
                        mRecyclerview.removeView(banner_frg01);
                        lvgv = 1;
                        floatingActionButton.setImageResource(R.drawable.btn_blue_qiehuan_1);

                    } else {
                        isSelected = true;
                        list_xrecyclerView.setVisibility(View.GONE);
                        mRecyclerview.setVisibility(View.VISIBLE);
                        // scrollToPosition();
                        list_xrecyclerView.removeView(banner_frg01);
                        lvgv = 0;
                        floatingActionButton.setImageResource(R.drawable.btn_icon_leibiao);
                    }

                }
            });
            //分享按钮
            dialog_tmall_floatingActionButton_share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (lvgv == 0) {
                        Log.e("数据Toady", Contig.checkedIndexltmalgrid + "-=====Contig.checkedIndex===id");
                        if (Contig.checkedIndexltmalgrid.size() == 0) {
                            Toast.makeText(getActivity(), "没有选择，请选择再分享！", Toast.LENGTH_SHORT).show();
                        } else {
                            for (int i = 0; i < Contig.checkedIndexltmalgrid.size(); i++)
                                idlist.add(jDcomjPlistlist.get(Contig.checkedIndexltmalgrid.get(i)).getId());

                            Log.e("数据Toady", idlist + "-===========id");


                            StringBuffer permission = new StringBuffer();
                            for (int i = 0; i < idlist.size(); i++) {
                                if (i == 0) {
                                    permission.append(idlist.get(i));
                                } else {
                                    permission.append("," + idlist.get(i));
                                }
                                Contig.listididtml.add(idlist.get(i));

                            }
//                    LoadStoreList(0, 0, Contig.listidid);

                            ApiManager
                                    .getInstance()
                                    .getApiServer()
                                    .getShareImage(SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN), AUniqueIdentifier.getDeviceId(getActivity()), permission.toString())
                                    .compose(ApiManager.<ShareImageBean>handlResult())
                                    .subscribe(new ObserverCallback<ShareImageBean>(getActivity()) {
                                        @Override
                                        protected void onSuccessful(ShareImageBean resp) {

                                            saveImage(resp.getPoster(), resp.getFailure(), SHARET_WXHY);
                                            if (Contig.checkedIndexltmalgrid.size() > 0) {
                                                for (int a = 0; a < Contig.checkedIndexltmalgrid.size(); a++) {
                                                    Contig.ShareListtmll.add(jDcomjPlistlist.get(Contig.checkedIndexltmalgrid.get(a)).getId());
                                                }
                                            }
                                            myRecycleViewAdapter = new TmallchoicenessAdapter(jDcomjPlistlist, getActivity(), "");
                                            mRecyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false));
                                            mRecyclerview.setAdapter(myRecycleViewAdapter);

                                        }

                                        @Override
                                        protected void onError(String code, String errMsg) {
                                            ToastUtils.showShortSafe(errMsg);
                                        }
                                    });
                            idlist.clear();
                        }
                    } else if (lvgv == 1) {
                        Log.e("数据Toady", Contig.checkedIndexltmallist + "-=====Contig.checkedIndex===id");
                        if (Contig.checkedIndexltmallist.size() == 0) {
                            Toast.makeText(getActivity(), "没有选择，请选择再分享！", Toast.LENGTH_SHORT).show();
                        } else {
                            for (int i = 0; i < Contig.checkedIndexltmallist.size(); i++)
                            idlistlv.add(jDcomjPlistlist.get(Contig.checkedIndexltmallist.get(i)).getId());

                            Log.e("数据Toady", idlistlv + "-===========id");
                            StringBuffer permission = new StringBuffer();
                            for (int i = 0; i < idlistlv.size(); i++) {
                                if (i == 0) {
                                    permission.append(idlistlv.get(i));
                                } else {
                                    permission.append("," + idlistlv.get(i));
                                }
                                Contig.listididlisttml.add(idlistlv.get(i));

                            }
//                    LoadStoreList(0, 0, Contig.listidid);

                            ApiManager
                                    .getInstance()
                                    .getApiServer()
                                    .getShareImage(SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN), AUniqueIdentifier.getDeviceId(getActivity()), permission.toString())
                                    .compose(ApiManager.<ShareImageBean>handlResult())
                                    .subscribe(new ObserverCallback<ShareImageBean>(getActivity()) {
                                        @Override
                                        protected void onSuccessful(ShareImageBean resp) {

                                            saveImage(resp.getPoster(), resp.getFailure(), SHARET_WXHY);
                                            if (Contig.checkedIndexltmallist.size() > 0) {
                                                for (int a = 0; a < Contig.checkedIndexltmallist.size(); a++) {
                                                    Contig.ShareListlvtmll.add(jDcomjPlistlist.get(Contig.checkedIndexltmallist.get(a)).getId());
                                                }
                                            }

                                            tmallchoicenessListAdapter = new TmallchoicenessListAdapter(getActivity(), jDcomjPlistlist, "");
                                            list_xrecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
                                            list_xrecyclerView.setAdapter(tmallchoicenessListAdapter);
                                        }

                                        @Override
                                        protected void onError(String code, String errMsg) {
                                            ToastUtils.showShortSafe(errMsg);
                                        }
                                    });
                            idlistlv.clear();
                        }
                    }
                }
            });

//        mRecyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                if (mRecyclerview.getLayoutManager() != null) {
//                    //getPositionAndOffset();
//                }
//            }
//        });
        }catch (Exception e){

        }
    }
    private void saveImage(List<String> imageUrls, final String cconten, final int type) {
try {
    Log.v("shiran", "------------saveImage------------");

    final LoadingDialog dialog = new LoadingDialog(getActivity());
    dialog.setContent("正在生成图片请稍后");


    Observable.just(imageUrls)
            .map(new Function<List<String>, List<String>>() {

                @Override
                public List<String> apply(List<String> list) throws Exception {

                    List<String> paths = new ArrayList<String>();
                    for (String s : list) {
                        FutureTarget<File> future = Glide.with(APP.getInstance())
                                .load(s)
                                .downloadOnly(500, 500);
                        File cacheFile = future.get();
                        String uuid = UUID.randomUUID().toString();
                        FileIOUtils.writeFileFromIS(savePath + uuid + ".jpg", new FileInputStream(cacheFile));
                        String pat = savePath + uuid + ".jpg";
                        paths.add(pat);

                    }


                    return paths;
                }
            }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<List<String>>() {
                @Override
                public void onSubscribe(Disposable d) {
                    if (dialog != null && !dialog.isShowing()) {
                        dialog.show();
                    }
                }

                @Override
                public void onNext(List<String> value) {
                    LogUtils.e(value);
                    if (dialog != null && dialog.isShowing()) {
                        dialog.dismiss();
                        dialog.cancel();
                    }

                    switch (type) {
                        case SHARET_WXHY:


                            ShareUtils.sharePicToFriendNoSDK(getActivity(), value);
//                                        ClipboardUtils.copyText(cconten);
//                                        ToastUtils.showShortSafe("内容已复制!");
                            break;
//                            case SHARET_WXPYQ:
//                                ShareUtils.share9PicsToWXCircle(getActivity(), "", value);
//                                break;
                        default:
                            break;
                    }
                }

                @Override
                public void onError(Throwable e) {
                    if (dialog != null && dialog.isShowing()) {
                        dialog.dismiss();
                        dialog.cancel();
                    }
                }

                @Override
                public void onComplete() {
                    if (dialog != null && dialog.isShowing()) {
                        dialog.dismiss();
                        dialog.cancel();
                    }
                }
            });
}catch (Exception e){}
    }
    private void initjplistHttp() {
        try {
            Log.e("优惠", GOODSURL);
            OkHttpUtils.post().url(GOODSURL).addParams("type", "jing").addParams("start", curr + "").addParams("length", 10 + "").build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    mRecyclerview.refreshComplete();
                    initData(response);
                    initDatalist(response);
                }
            });
        }catch (Exception e){

        }
            }

    private void initDatalist(String response) {

        try {
            list_xrecyclerView.refreshComplete();
            Gson gson = new Gson();
            final JDcomjPlistBean jDcomjPlistBean = gson.fromJson(response, JDcomjPlistBean.class);
            if (jDcomjPlistBean != null && jDcomjPlistBean.getData() != null && jDcomjPlistBean.getData().getList() != null) {
//                if (curr == 0) {
//                    jDcomjPlistlist.clear();
//                }
                jDcomjPlistlist.addAll(jDcomjPlistBean.getData().getList());
                myRecycleViewAdapter.notifyDataSetChanged();
                tmallchoicenessListAdapter.notifyDataSetChanged();
                tmallchoicenessListAdapter.setmMylistItemclickListener(new TmallchoicenessListAdapter.MylistItemclickListener() {
                    @Override
                    public void listitemclick(View view, int position) {
                        if (Contig.typex.equals("yh")) {
                            Intent intent = new Intent(getActivity(), Red_PacketActivity.class);
                            intent.putExtra("shopid", jDcomjPlistlist.get(position).getId());
                            intent.putExtra("typex", Contig.typex);
                            startActivity(intent);
                        } else {
                            location = position;
                            if (Contig.typex.equals("yh")) {
                                Intent intent = new Intent(getActivity(), Red_PacketActivity.class);
                                intent.putExtra("shopid", jDcomjPlistlist.get(position).getId());
                                intent.putExtra("typex", Contig.typex);
                                startActivity(intent);
                            } else {
                                if (Contig.typex.equals("jd")) {
                                    if (jDcomjPlistlist.get(position).getCouponMoney().equals("")) {
                                        initJDputong();
                                        if(KeplerApiManager.getWebViewService().isKeplerLogined()){
                                            try {
                                                KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                            } catch (KeplerBufferOverflowException e) {
                                                e.printStackTrace();
                                            }

                                        }else {
                                            final LoginListener mLoginListener = new LoginListener() {
                                                @Override
                                                public void authSuccess(Object token) {
                                                    // TODO Auto-generated method stub
                                                    Toast.makeText(getActivity(), token + "", Toast.LENGTH_SHORT).show();
                                                    //                showToken();

                                                    if (KeplerApiManager.getWebViewService().isKeplerLogined()) {
                                                        try {
                                                            KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                        } catch (KeplerBufferOverflowException e) {
                                                            e.printStackTrace();
                                                        }
                                                    }

                                                }

                                                @Override
                                                public void authFailed(int errorCode) {
                                                    // TODO Auto-generated method stub
                                                    switch (errorCode) {
                                                        case KeplerApiManager.KeplerApiManagerLoginErr_Init:// 初始化失败 break;
                                                        case KeplerApiManager.KeplerApiManagerLoginErr_InitIng:// 初始化没有完成 break;
                                                        case KeplerApiManager.KeplerApiManagerLoginErr_openH5authPageURLSettingNull://跳转url // 为null break;
                                                        case KeplerApiManager.KeplerApiManagerLoginErr_getTokenErr:// 获取失败(OAuth授权之后，获取cookie过程出错) break;
                                                        case KeplerApiManager.KeplerApiManagerLoginErr_User_Cancel:// 用户取消 break;
                                                        case KeplerApiManager.KeplerApiManagerLoginErr_AuthErr_ActivityOpen://打开授权页面失败 break;
                                                        default:
                                                            break;
                                                    }
//                Toast.makeText(AllOrderActivity.this, errorCode + ":authFailed", Toast.LENGTH_SHORT).show();
                                                    Log.e("jdSHOUQUAN", errorCode + ":authFailed");
                                                }
                                            };
                                            KeplerApiManager.getWebViewService().login(getActivity(), mLoginListener);
                                        }
//                                        Intent intent = new Intent(getActivity(), HtmlActivity.class);
//                                        intent.putExtra("data", datalisnajie);
//                                        intent.putExtra("shopid", jDcomjPlistlist.get(location).getId());
//                                        startActivity(intent);
                                    }
                                    if (jDcomjPlistlist.get(position).getCouponMoney() != "") {
                                        if (jDcomjPlistlist.get(position).getCouponMoney().equals("0.0")) {

                                            Map<String, String> map = new HashMap<>();
                                            map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                            map.put("spuId", jDcomjPlistlist.get(location).getId());
                                            map.put("appid", AUniqueIdentifier.getDeviceId(getActivity()));

                                            OkHttpUtils.post().params(map).url(GETCONURLURL).build().execute(new StringCallback() {
                                                @Override
                                                public void onError(Request request, Exception e) {

                                                }

                                                @Override
                                                public void onResponse(String response) {
                                                    Log.e("优惠二和一数据", response);
                                                    Gson gson = new Gson();
                                                    jDlianjiebean = gson.fromJson(response, JDlianjie.class);
                                                    datayouhui = jDlianjiebean.getData();
                                                    if (jDlianjiebean.getCode().equals("10000")) {
                                                        Intent intent = new Intent(getActivity(), JDred_packetActivity.class);
                                                        intent.putExtra("shopid", jDcomjPlistlist.get(location).getId());
                                                        startActivity(intent);
                                                    }
                                                }
                                            });

                                        } else {
                                            Log.e("优惠二和一链接接口", GETCONURLURL + "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&spuId=" + jDcomjPlistlist.get(location).getId() + "&appid=" + AUniqueIdentifier.getDeviceId(getActivity()));
                                            Map<String, String> map = new HashMap<>();
                                            map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                            map.put("spuId", jDcomjPlistlist.get(location).getId());
                                            map.put("appid", AUniqueIdentifier.getDeviceId(getActivity()));

                                            OkHttpUtils.post().params(map).url(GETCONURLURL).build().execute(new StringCallback() {
                                                @Override
                                                public void onError(Request request, Exception e) {

                                                }

                                                @Override
                                                public void onResponse(String response) {
                                                    Log.e("优惠二和一数据9", response);
                                                    Gson gson = new Gson();
                                                    jDlianjiebean = gson.fromJson(response, JDlianjie.class);
                                                    datayouhui = jDlianjiebean.getData();
                                                    if (jDlianjiebean.getCode().equals("10000")) {
                                                        if (jDlianjiebean.getData() != null) {
                                                            if(KeplerApiManager.getWebViewService().isKeplerLogined()){
                                                                try {
                                                                    KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                                } catch (KeplerBufferOverflowException e) {
                                                                    e.printStackTrace();
                                                                }

                                                            }else {
                                                                final LoginListener mLoginListener = new LoginListener() {
                                                                    @Override
                                                                    public void authSuccess(Object token) {
                                                                        // TODO Auto-generated method stub
                                                                        Toast.makeText(getActivity(), token + "", Toast.LENGTH_SHORT).show();
                                                                        //                showToken();

                                                                        if (KeplerApiManager.getWebViewService().isKeplerLogined()) {
                                                                            try {
                                                                                KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                                            } catch (KeplerBufferOverflowException e) {
                                                                                e.printStackTrace();
                                                                            }
                                                                        }

                                                                    }

                                                                    @Override
                                                                    public void authFailed(int errorCode) {
                                                                        // TODO Auto-generated method stub
                                                                        switch (errorCode) {
                                                                            case KeplerApiManager.KeplerApiManagerLoginErr_Init:// 初始化失败 break;
                                                                            case KeplerApiManager.KeplerApiManagerLoginErr_InitIng:// 初始化没有完成 break;
                                                                            case KeplerApiManager.KeplerApiManagerLoginErr_openH5authPageURLSettingNull://跳转url // 为null break;
                                                                            case KeplerApiManager.KeplerApiManagerLoginErr_getTokenErr:// 获取失败(OAuth授权之后，获取cookie过程出错) break;
                                                                            case KeplerApiManager.KeplerApiManagerLoginErr_User_Cancel:// 用户取消 break;
                                                                            case KeplerApiManager.KeplerApiManagerLoginErr_AuthErr_ActivityOpen://打开授权页面失败 break;
                                                                            default:
                                                                                break;
                                                                        }
//                Toast.makeText(AllOrderActivity.this, errorCode + ":authFailed", Toast.LENGTH_SHORT).show();
                                                                        Log.e("jdSHOUQUAN", errorCode + ":authFailed");
                                                                    }
                                                                };
                                                                KeplerApiManager.getWebViewService().login(getActivity(), mLoginListener);
                                                            }
//                                                            Intent intent = new Intent(getActivity(), HtmlActivity.class);
//                                                            intent.putExtra("shopid", jDcomjPlistlist.get(location).getId());
//                                                            intent.putExtra("data", jDlianjiebean.getData());
//                                                            startActivity(intent);

                                                        }
                                                    }
                                                }
                                            });
                                        }
                                    }


                                }
                            }
                        }
                    }
                });
            }
        }catch (Exception e){

        }
        }

    private void initData(String response) {
        try {
            mRecyclerview.refreshComplete();
            Log.e("精选111", response);
            Gson gson = new Gson();
            final JDcomjPlistBean jDcomjPlistBean = gson.fromJson(response, JDcomjPlistBean.class);
            if (jDcomjPlistBean != null && jDcomjPlistBean.getData() != null && jDcomjPlistBean.getData().getList() != null) {
//                if (curr == 0) {
//                    jDcomjPlistlist.clear();
//                }
                jDcomjPlistlist.addAll(jDcomjPlistBean.getData().getList());
                myRecycleViewAdapter.notifyDataSetChanged();
                myRecycleViewAdapter.setmMyItemclickListener(new TmallchoicenessAdapter.MyItemclickListener() {
                    @Override
                    public void itemclick(View view, int position) {
                        location = position;
                        if (Contig.typex.equals("yh")) {
                            Intent intent = new Intent(getActivity(), Red_PacketActivity.class);
                            intent.putExtra("shopid", jDcomjPlistlist.get(position).getId());
                            intent.putExtra("typex", Contig.typex);
                            startActivity(intent);

                        } else {
                            if (Contig.typex.equals("jd")) {
                                if (jDcomjPlistlist.get(position).getCouponMoney().equals("")) {
                                    initJDputong();
                                    if(KeplerApiManager.getWebViewService().isKeplerLogined()){
                                        try {
                                            KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                        } catch (KeplerBufferOverflowException e) {
                                            e.printStackTrace();
                                        }

                                    }else {
                                        final LoginListener mLoginListener = new LoginListener() {
                                            @Override
                                            public void authSuccess(Object token) {
                                                // TODO Auto-generated method stub
                                                Toast.makeText(getActivity(), token + "", Toast.LENGTH_SHORT).show();
                                                //                showToken();

                                                if (KeplerApiManager.getWebViewService().isKeplerLogined()) {
                                                    try {
                                                        KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                    } catch (KeplerBufferOverflowException e) {
                                                        e.printStackTrace();
                                                    }
                                                }

                                            }

                                            @Override
                                            public void authFailed(int errorCode) {
                                                // TODO Auto-generated method stub
                                                switch (errorCode) {
                                                    case KeplerApiManager.KeplerApiManagerLoginErr_Init:// 初始化失败 break;
                                                    case KeplerApiManager.KeplerApiManagerLoginErr_InitIng:// 初始化没有完成 break;
                                                    case KeplerApiManager.KeplerApiManagerLoginErr_openH5authPageURLSettingNull://跳转url // 为null break;
                                                    case KeplerApiManager.KeplerApiManagerLoginErr_getTokenErr:// 获取失败(OAuth授权之后，获取cookie过程出错) break;
                                                    case KeplerApiManager.KeplerApiManagerLoginErr_User_Cancel:// 用户取消 break;
                                                    case KeplerApiManager.KeplerApiManagerLoginErr_AuthErr_ActivityOpen://打开授权页面失败 break;
                                                    default:
                                                        break;
                                                }
//                Toast.makeText(AllOrderActivity.this, errorCode + ":authFailed", Toast.LENGTH_SHORT).show();
                                                Log.e("jdSHOUQUAN", errorCode + ":authFailed");
                                            }
                                        };
                                        KeplerApiManager.getWebViewService().login(getActivity(), mLoginListener);
                                    }
//                                    Intent intent = new Intent(getActivity(), HtmlActivity.class);
//                                    intent.putExtra("data", datalisnajie);
//                                    intent.putExtra("shopid", jDcomjPlistlist.get(location).getId());
//                                    startActivity(intent);
                                }
                                if (jDcomjPlistlist.get(position).getCouponMoney() != "") {
                                    if (jDcomjPlistlist.get(position).getCouponMoney().equals("0.0")) {
                                        Map<String, String> map = new HashMap<>();
                                        map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                        map.put("spuId", jDcomjPlistlist.get(location).getId());
                                        map.put("appid", AUniqueIdentifier.getDeviceId(getActivity()));
                                        Log.e("优惠二和一链接接口", GETCONURLURL + "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&spuId=" + jDcomjPlistlist.get(location).getId() + "&appid=" + AUniqueIdentifier.getDeviceId(getActivity()));
                                        OkHttpUtils.post().params(map).url(GETCONURLURL).build().execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {

                                            }

                                            @Override
                                            public void onResponse(String response) {
                                                Log.e("优惠二和一数据", response);
                                                Gson gson = new Gson();
                                                jDlianjiebean = gson.fromJson(response, JDlianjie.class);
                                                datayouhui = jDlianjiebean.getData();
                                                if (jDlianjiebean.getCode().equals("10000")) {
                                                    Intent intent = new Intent(getActivity(), JDred_packetActivity.class);
                                                    intent.putExtra("shopid", jDcomjPlistlist.get(location).getId());
                                                    startActivity(intent);
                                                }
                                            }
                                        });

                                    } else {
                                        Map<String, String> map = new HashMap<>();
                                        map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                        map.put("spuId", jDcomjPlistlist.get(location).getId());
                                        map.put("appid", AUniqueIdentifier.getDeviceId(getActivity()));

                                        OkHttpUtils.post().params(map).url(GETCONURLURL).build().execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {

                                            }

                                            @Override
                                            public void onResponse(String response) {
                                                Log.e("优惠二和一数据", response);
                                                Gson gson = new Gson();
                                                jDlianjiebean = gson.fromJson(response, JDlianjie.class);
                                                datayouhui = jDlianjiebean.getData();
                                                if (jDlianjiebean.getCode().equals("10000")) {
                                                    if (jDlianjiebean.getData() != null) {
//                                                        Intent intent = new Intent(getActivity(), HtmlActivity.class);
//                                                        intent.putExtra("shopid", jDcomjPlistlist.get(location).getId());
//                                                        intent.putExtra("data", datayouhui);
//                                                        startActivity(intent);
                                                        if(KeplerApiManager.getWebViewService().isKeplerLogined()){
                                                            try {
                                                                KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                            } catch (KeplerBufferOverflowException e) {
                                                                e.printStackTrace();
                                                            }

                                                        }else {
                                                            final LoginListener mLoginListener = new LoginListener() {
                                                                @Override
                                                                public void authSuccess(Object token) {
                                                                    // TODO Auto-generated method stub
                                                                    Toast.makeText(getActivity(), token + "", Toast.LENGTH_SHORT).show();
                                                                    //                showToken();

                                                                    if (KeplerApiManager.getWebViewService().isKeplerLogined()) {
                                                                        try {
                                                                            KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                                        } catch (KeplerBufferOverflowException e) {
                                                                            e.printStackTrace();
                                                                        }
                                                                    }

                                                                }

                                                                @Override
                                                                public void authFailed(int errorCode) {
                                                                    // TODO Auto-generated method stub
                                                                    switch (errorCode) {
                                                                        case KeplerApiManager.KeplerApiManagerLoginErr_Init:// 初始化失败 break;
                                                                        case KeplerApiManager.KeplerApiManagerLoginErr_InitIng:// 初始化没有完成 break;
                                                                        case KeplerApiManager.KeplerApiManagerLoginErr_openH5authPageURLSettingNull://跳转url // 为null break;
                                                                        case KeplerApiManager.KeplerApiManagerLoginErr_getTokenErr:// 获取失败(OAuth授权之后，获取cookie过程出错) break;
                                                                        case KeplerApiManager.KeplerApiManagerLoginErr_User_Cancel:// 用户取消 break;
                                                                        case KeplerApiManager.KeplerApiManagerLoginErr_AuthErr_ActivityOpen://打开授权页面失败 break;
                                                                        default:
                                                                            break;
                                                                    }
//                Toast.makeText(AllOrderActivity.this, errorCode + ":authFailed", Toast.LENGTH_SHORT).show();
                                                                    Log.e("jdSHOUQUAN", errorCode + ":authFailed");
                                                                }
                                                            };
                                                            KeplerApiManager.getWebViewService().login(getActivity(), mLoginListener);
                                                        }
                                                    }
                                                }
                                            }

                                        });


                                    }
                                }


                            }
                        }

                    }
                });


            }
        }catch (Exception e){

        }
    }

    //普通二和一链接
    private void initJDputong(){
        try {
            Map<String, String> map = new HashMap<String, String>();
            map.put("spuId", jDcomjPlistlist.get(location).getId());
            map.put("appid", AUniqueIdentifier.getDeviceId(getActivity()));
            map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
            Log.e("京东二合一转链", SYNCJDSPREADURL + "?spuId=" + jDcomjPlistlist.get(location).getId() + "&appId=" + AUniqueIdentifier.getDeviceId(getActivity()) + "&token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
            OkHttpUtils.post().params(map).url(SYNCJDSPREADURL).build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {

                }
                @Override
                public void onResponse(String response) {
                    Log.e("fgdgfe", response);
                    Gson gson = new Gson();
                    JDlianjie jDlianjie = gson.fromJson(response, JDlianjie.class);
                    datalisnajie = jDlianjie.getData();
                    Log.e("kfjsdkghdfk", datalisnajie);

                }
            });
        }catch (Exception e){

        }
}
    //特卖商品网络请求
    private void initSpeciallistHttp() {
        try {
            OkHttpUtils.post().addParams("type", "special").url(GOODSURL).build().execute(new StringCallback() {


                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    Gson json = new Gson();
                    specialGddodsBean = json.fromJson(response, SpecialGddodsBean.class);
                    if(specialGddodsBean.getCode().equals("10000")) {
                        List<SpecialGddodsBean.DataBean.ListBean> list = specialGddodsBean.getData().getList();
                        List<SpecialGddodsBean.DataBean.ListBean> list2 = new ArrayList<SpecialGddodsBean.DataBean.ListBean>();
                        for (int i = 0; i < list.size(); i++) {
                            if (i < 6) {
                                list2.add(list.get(i));
                            } else {
                                break;
                            }

                        }
                        Tnall_specialAdapter Tnall_specialAdapter = new Tnall_specialAdapter(list2, getActivity());
                        tmall_seckill_recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false));
                        tmall_seckill_recyclerView.setAdapter(Tnall_specialAdapter);
                        Tnall_specialAdapter.setmMyItemclickListener(new Tnall_specialAdapter.MyItemclickListener() {
                            @Override
                            public void itemclick(View view, int position) {
                                if (Contig.typex.equals("yh")) {
                                    Intent intent = new Intent(getActivity(), Red_PacketActivity.class);
                                    intent.putExtra("shopid", jDcomjPlistlist.get(position).getId());
                                    intent.putExtra("typex", Contig.typex);
                                    startActivity(intent);
                                } else {
                                    location = position;
                                    if (Contig.typex.equals("jd")) {
                                        if (jDcomjPlistlist.get(position).getCouponMoney().equals("")) {
                                            initJDputong();
                                            if(KeplerApiManager.getWebViewService().isKeplerLogined()){
                                                try {
                                                    KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                } catch (KeplerBufferOverflowException e) {
                                                    e.printStackTrace();
                                                }

                                            }else {
                                                final LoginListener mLoginListener = new LoginListener() {
                                                    @Override
                                                    public void authSuccess(Object token) {
                                                        // TODO Auto-generated method stub
                                                        Toast.makeText(getActivity(), token + "", Toast.LENGTH_SHORT).show();
                                                        //                showToken();

                                                        if (KeplerApiManager.getWebViewService().isKeplerLogined()) {
                                                            try {
                                                                KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                            } catch (KeplerBufferOverflowException e) {
                                                                e.printStackTrace();
                                                            }
                                                        }

                                                    }

                                                    @Override
                                                    public void authFailed(int errorCode) {
                                                        // TODO Auto-generated method stub
                                                        switch (errorCode) {
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_Init:// 初始化失败 break;
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_InitIng:// 初始化没有完成 break;
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_openH5authPageURLSettingNull://跳转url // 为null break;
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_getTokenErr:// 获取失败(OAuth授权之后，获取cookie过程出错) break;
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_User_Cancel:// 用户取消 break;
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_AuthErr_ActivityOpen://打开授权页面失败 break;
                                                            default:
                                                                break;
                                                        }
//                Toast.makeText(AllOrderActivity.this, errorCode + ":authFailed", Toast.LENGTH_SHORT).show();
                                                        Log.e("jdSHOUQUAN", errorCode + ":authFailed");
                                                    }
                                                };
                                                KeplerApiManager.getWebViewService().login(getActivity(), mLoginListener);
                                            }
//                                            Intent intent = new Intent(getActivity(), HtmlActivity.class);
//                                            intent.putExtra("data", datalisnajie);
//                                            intent.putExtra("shopid", jDcomjPlistlist.get(location).getId());
//                                            startActivity(intent);
                                        }
                                        if (jDcomjPlistlist.get(position).getCouponMoney() != "") {
                                            if (jDcomjPlistlist.get(position).getCouponMoney().equals("0.0")) {
                                                Log.e("优惠二和一链接接口", GETCONURLURL + "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&spuId=" + jDcomjPlistlist.get(location).getId() + "&appid=" + AUniqueIdentifier.getDeviceId(getActivity()));
                                                Map<String, String> map = new HashMap<>();
                                                map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                                map.put("spuId", jDcomjPlistlist.get(location).getId());
                                                map.put("appid", AUniqueIdentifier.getDeviceId(getActivity()));
                                                Log.e("优惠二和一链接接口", GETCONURLURL + "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&spuId=" + jDcomjPlistlist.get(location).getId() + "&appid=" + AUniqueIdentifier.getDeviceId(getActivity()));
                                                OkHttpUtils.post().params(map).url(GETCONURLURL).build().execute(new StringCallback() {
                                                    @Override
                                                    public void onError(Request request, Exception e) {

                                                    }

                                                    @Override
                                                    public void onResponse(String response) {
                                                        Log.e("优惠二和一数据", response);
                                                        Gson gson = new Gson();
                                                        jDlianjiebean = gson.fromJson(response, JDlianjie.class);
                                                        datayouhui = jDlianjiebean.getData();
                                                        if (jDlianjiebean.getCode().equals("10000")) {
                                                            Intent intent = new Intent(getActivity(), JDred_packetActivity.class);
                                                            intent.putExtra("shopid", jDcomjPlistlist.get(location).getId());
                                                            startActivity(intent);
                                                        }
                                                    }
                                                });

                                            } else {
                                                Log.e("优惠二和一链接接口", GETCONURLURL + "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&spuId=" + jDcomjPlistlist.get(location).getId() + "&appid=" + AUniqueIdentifier.getDeviceId(getActivity()));
                                                Map<String, String> map = new HashMap<>();
                                                map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                                map.put("spuId", jDcomjPlistlist.get(location).getId());
                                                map.put("appid", AUniqueIdentifier.getDeviceId(getActivity()));

                                                OkHttpUtils.post().params(map).url(GETCONURLURL).build().execute(new StringCallback() {
                                                    @Override
                                                    public void onError(Request request, Exception e) {

                                                    }

                                                    @Override
                                                    public void onResponse(String response) {
                                                        Log.e("优惠二和一数据", response);
                                                        Gson gson = new Gson();
                                                        jDlianjiebean = gson.fromJson(response, JDlianjie.class);
                                                        datayouhui = jDlianjiebean.getData();
                                                        if (jDlianjiebean.getCode().equals("10000")) {
                                                            if (jDlianjiebean.getData() != null) {
                                                                if(KeplerApiManager.getWebViewService().isKeplerLogined()){
                                                                    try {
                                                                        KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                                    } catch (KeplerBufferOverflowException e) {
                                                                        e.printStackTrace();
                                                                    }

                                                                }else {
                                                                    final LoginListener mLoginListener = new LoginListener() {
                                                                        @Override
                                                                        public void authSuccess(Object token) {
                                                                            // TODO Auto-generated method stub
                                                                            Toast.makeText(getActivity(), token + "", Toast.LENGTH_SHORT).show();
                                                                            //                showToken();

                                                                            if (KeplerApiManager.getWebViewService().isKeplerLogined()) {
                                                                                try {
                                                                                    KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                                                } catch (KeplerBufferOverflowException e) {
                                                                                    e.printStackTrace();
                                                                                }
                                                                            }

                                                                        }

                                                                        @Override
                                                                        public void authFailed(int errorCode) {
                                                                            // TODO Auto-generated method stub
                                                                            switch (errorCode) {
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_Init:// 初始化失败 break;
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_InitIng:// 初始化没有完成 break;
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_openH5authPageURLSettingNull://跳转url // 为null break;
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_getTokenErr:// 获取失败(OAuth授权之后，获取cookie过程出错) break;
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_User_Cancel:// 用户取消 break;
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_AuthErr_ActivityOpen://打开授权页面失败 break;
                                                                                default:
                                                                                    break;
                                                                            }
//                Toast.makeText(AllOrderActivity.this, errorCode + ":authFailed", Toast.LENGTH_SHORT).show();
                                                                            Log.e("jdSHOUQUAN", errorCode + ":authFailed");
                                                                        }
                                                                    };
                                                                    KeplerApiManager.getWebViewService().login(getActivity(), mLoginListener);
                                                                }
//                                                                Intent intent = new Intent(getActivity(), HtmlActivity.class);
//                                                                intent.putExtra("shopid", jDcomjPlistlist.get(location).getId());
//                                                                intent.putExtra("data", datayouhui);
//                                                                startActivity(intent);
                                                            }
                                                        }
                                                    }
                                                });
                                            }
                                        }

                                    }
                                }
                            }
                        });
                    }else{
                        Toast.makeText(getActivity(),specialGddodsBean.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }catch (Exception e){

        }
    }

    //广告位网络请求
    private void initAdvertisingHttp() {
        Log.e("jDcomAdvertisingimge","jDcomAdvertisingimge========"+ADVERTSURL+"?address=002");
        try {
            OkHttpUtils.post().url(ADVERTSURL).addParams("address", "002").build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();

                    JDcomAdvertisingBean jDcomAdvertisingBean = gson.fromJson(response, JDcomAdvertisingBean.class);
                    if (jDcomAdvertisingBean.getCode().equals("10000")) {
                        jDcomAdvertisinglist = new ArrayList<JDcomAdvertisingBean.DataBean>();

                        jDcomAdvertisinglist.addAll(jDcomAdvertisingBean.getData());
                        Log.e("", "jDcomAdvertisingimge========jDcomAdvertisinglist " + jDcomAdvertisinglist);
                        for (JDcomAdvertisingBean.DataBean jDcomAdvertisingimgeURl : jDcomAdvertisinglist) {
                            jDcomAdvertisingimge = jDcomAdvertisingimgeURl.getImg();
                            jDcomAdvertisingID = jDcomAdvertisingimgeURl.getId();
                        }
                        Log.e("", "jDcomAdvertisingimge======== " + jDcomAdvertisingimge);
                        if(jDcomAdvertisingBean.getData().equals("")){
                            jDcomAdvertising_imge.setVisibility(View.GONE);
                        }else {
                            if (jDcomAdvertisinglist.size() == 0) {
                                jDcomAdvertising_imge.setVisibility(View.GONE);
                            } else {
                                jDcomAdvertising_imge.setVisibility(View.VISIBLE);
                                Glide.with(getActivity()).load(jDcomAdvertisingimge).into(jDcomAdvertising_imge);
                            }
                        }
                        jDcomAdvertising_imge.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                Intent intent = new Intent(getActivity(), Specialactivity.class);
                                intent.putExtra("urlmarket", ApiManager.MARKET_ADVERTDETAIL);
                                intent.putExtra("id", jDcomAdvertisingID);
                                startActivity(intent);
                            }
                        });
                    }else{
                        Toast.makeText(getActivity(),jDcomAdvertisingBean.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }catch (Exception e){

        }
    }


    //专区网络请求
    private void initOEMHttp() {
        try {
            OkHttpUtils
                    .post()
                    .url(MODELSURL)
                    .addParams("categoryType", "002")
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                        }

                        @Override
                        public void onResponse(String response) {
                            Gson json = new Gson();
                            JDcomOEMBeam jDcomOEMBeam = json.fromJson(response, JDcomOEMBeam.class);
                            oemlist = new ArrayList<JDcomOEMBeam.DataBean>();
                            oemlist.addAll(jDcomOEMBeam.getData());
                            oeMimgeUrllist = new ArrayList<String>();
                            for (JDcomOEMBeam.DataBean oemimgeUrl : oemlist) {
                                oeMimgeUrllist.add(oemimgeUrl.getImg());
                            }


                            Log.e("京东首页布局图片","图片===="+jDcomOEMBeam.getData().get(1).getImg());
                            UtilsImage.uploadimageview(iv_adv1,jDcomOEMBeam.getData().get(0).getImg(),null,getActivity());
                            UtilsImage.uploadimageview(iv_adv2,jDcomOEMBeam.getData().get(1).getImg(),null,getActivity());
                            UtilsImage.uploadimageview(iv_adv3,jDcomOEMBeam.getData().get(2).getImg(),null,getActivity());

//                            Tmall_homepageAdapter Tmall_homepageAdapter = new Tmall_homepageAdapter(getActivity(), oeMimgeUrllist);
//                            tmall_recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false));
//                            tmall_recyclerView.setAdapter(Tmall_homepageAdapter);
//                            Tmall_homepageAdapter.setmMyItemclickListener(new Tmall_homepageAdapter.MyItemclickListener() {
//                                @Override
//                                public void itemclick(View view, int position) {
//                                    Toast.makeText(getActivity(), "专区", Toast.LENGTH_LONG).show();
//                                }
//                            });
                        }
                    });

        }catch (Exception e){

        }

    }

    //setbanner轮播图数据
    private void initBannerView() {
        try {
            tmall_banner.setImageLoader(new GlideImageLoader());
            //设置图片集合
            tmall_banner.setImages(bannerimge);
            //banner设置方法全部调用完毕时最后调用
            tmall_banner.start();
            tmall_banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {
                    if(bannerlist.get(position).getLinkType().equals("h5")){
                        Intent intent=new Intent(getActivity(),RankingRanNo1HtmlActivity.class);
                        intent.putExtra("url",bannerlist.get(position).getValue());
                        intent.putExtra("title","专题");
                        startActivity(intent);
                    }else if(bannerlist.get(position).getLinkType().equals("app")){
                        Intent intent=new Intent(getActivity(),Specialactivity.class);
                        intent.putExtra("urlmarket",ApiManager.MARKET_BANNERDETAIL);
                        intent.putExtra("id",bannerlist.get(position).getId());
                        startActivity(intent);
                    }

//                Intent intent=new Intent(getActivity(),ShoppingCartActivity.class);
//                intent.putExtra("shopid",oemlist.get(position).getId());
//                startActivity(intent);
                }
            });
        }catch (Exception e){

        }
    }

    //banner网络请求
    private void initbannerHttp() {
        try {


            OkHttpUtils
                    .get()
                    .url(BANNERSURL)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                        }

                        @Override
                        public void onResponse(String response) {
                            Gson json = new Gson();
                            bannerlist = new ArrayList<>();

                            JDcomBannerbean BannerBean = json.fromJson(response, JDcomBannerbean.class);
                            if(BannerBean.getCode().equals("10000")) {
                                Log.e("优惠", BANNERSURL);
                                bannerlist.addAll(BannerBean.getData());
                                bannerimge = new ArrayList<String>();
                                for (JDcomBannerbean.DataBean bean : bannerlist) {
                                    bannerimge.add(bean.getImg());
                                }
                                initBannerView();

                            }else{
                                Toast.makeText(getActivity(),BannerBean.getMsg(),Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }catch (Exception e){}
    }






    public void getPositionAndOffset() {
        try {
            LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerview.getLayoutManager();
            //获取可视的第一个view
            View topView = layoutManager.getChildAt(0);
            if (topView != null) {
                //获取与该view的顶部的偏移量
                lastOffset = topView.getTop();
                //得到该View的数组位置
                lastPosition = layoutManager.getPosition(topView);
                sharedPreferences = getActivity().getSharedPreferences("key", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("lastOffset", lastOffset);
                editor.putInt("lastPosition", lastPosition);
                editor.commit();


            }
        }catch (Exception e){

        }
    }

    @Override
    public void onPause() {
        super.onPause();
        //getPositionAndOffset();

    }



    //  解决mRecyclerview没法保存状态
//    private void scrollToPosition() {
//
//        sharedPreferences = getActivity().getSharedPreferences("key", Activity.MODE_PRIVATE);
//
//        lastOffset = sharedPreferences.getInt("lastOffset", 0);
//
//        lastPosition = sharedPreferences.getInt("lastPosition", 0);
//
//        if (mRecyclerview.getLayoutManager() != null && lastPosition >= 0) {
//
//            ((LinearLayoutManager) mRecyclerview.getLayoutManager()).scrollToPositionWithOffset(lastPosition, lastOffset);
//
//        }
//
//    }

    public void getData() {
        Log.e("TmallhomepageFragment","getData()"+GOODSURL+"?type=hot");
        try {
            OkHttpUtils.post().addParams("type", "hot").url(GOODSURL).build().execute(new StringCallback() {

                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    Log.e("fsre", response);
                    Gson gson = new Gson();
                    JDcomHotlistBean jDcomHotlistBean = gson.fromJson(response, JDcomHotlistBean.class);
                    if (jDcomHotlistBean.getCode().equals("10000")) {
                        jDcomHotlist = new ArrayList<JDcomHotlistBean.DataBean.ListBean>();
                        jDcomHotlist.addAll(jDcomHotlistBean.getData().getList());
                        Tmall_homepage_cehuaAdapter Tmall_homepage_cehuaAdapter = new Tmall_homepage_cehuaAdapter(jDcomHotlist, getActivity());
                        cus.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayout.HORIZONTAL, false));
                        cus.setAdapter(Tmall_homepage_cehuaAdapter);
                        Tmall_homepage_cehuaAdapter.setmMyItemclickListener(new Tmall_homepage_cehuaAdapter.MyItemclickListener() {
                            @Override
                            public void itemclick(View view, int position) {
                                if (Contig.typex.equals("yh")) {
                                    Intent intent = new Intent(getActivity(), Red_PacketActivity.class);
                                    intent.putExtra("shopid", jDcomHotlist.get(position).getId());
                                    intent.putExtra("typex", Contig.typex);
                                    startActivity(intent);
                                } else {
                                    location = position;
                                    if (Contig.typex.equals("jd")) {
                                        if (jDcomHotlist.get(position).getCouponMoney().equals("")) {
                                            initJDputong();
                                            if(KeplerApiManager.getWebViewService().isKeplerLogined()){
                                                try {
                                                    KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                } catch (KeplerBufferOverflowException e) {
                                                    e.printStackTrace();
                                                }

                                            }else {
                                                final LoginListener mLoginListener = new LoginListener() {
                                                    @Override
                                                    public void authSuccess(Object token) {
                                                        // TODO Auto-generated method stub
                                                        Toast.makeText(getActivity(), token + "", Toast.LENGTH_SHORT).show();
                                                        //                showToken();

                                                        if (KeplerApiManager.getWebViewService().isKeplerLogined()) {
                                                            try {
                                                                KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                            } catch (KeplerBufferOverflowException e) {
                                                                e.printStackTrace();
                                                            }
                                                        }

                                                    }

                                                    @Override
                                                    public void authFailed(int errorCode) {
                                                        // TODO Auto-generated method stub
                                                        switch (errorCode) {
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_Init:// 初始化失败 break;
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_InitIng:// 初始化没有完成 break;
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_openH5authPageURLSettingNull://跳转url // 为null break;
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_getTokenErr:// 获取失败(OAuth授权之后，获取cookie过程出错) break;
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_User_Cancel:// 用户取消 break;
                                                            case KeplerApiManager.KeplerApiManagerLoginErr_AuthErr_ActivityOpen://打开授权页面失败 break;
                                                            default:
                                                                break;
                                                        }
//                Toast.makeText(AllOrderActivity.this, errorCode + ":authFailed", Toast.LENGTH_SHORT).show();
                                                        Log.e("jdSHOUQUAN", errorCode + ":authFailed");
                                                    }
                                                };
                                                KeplerApiManager.getWebViewService().login(getActivity(), mLoginListener);
                                            }
//                                            Intent intent = new Intent(getActivity(), HtmlActivity.class);
//                                            intent.putExtra("data", datalisnajie);
//                                            intent.putExtra("shopid", jDcomHotlist.get(location).getId());
//                                            startActivity(intent);
                                        }
                                        if (jDcomHotlist.get(position).getCouponMoney() != "") {
                                            if (jDcomHotlist.get(position).getCouponMoney().equals("0.0")) {
                                                Map<String, String> map = new HashMap<>();
                                                map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                                map.put("spuId", jDcomjPlistlist.get(location).getId());
                                                map.put("appid", AUniqueIdentifier.getDeviceId(getActivity()));
                                                Log.e("优惠二和一链接接口", GETCONURLURL + "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&spuId=" + jDcomjPlistlist.get(location).getId() + "&appid=" + AUniqueIdentifier.getDeviceId(getActivity()));
                                                OkHttpUtils.post().params(map).url(GETCONURLURL).build().execute(new StringCallback() {
                                                    @Override
                                                    public void onError(Request request, Exception e) {

                                                    }

                                                    @Override
                                                    public void onResponse(String response) {
                                                        Log.e("优惠二和一数据", response);
                                                        Gson gson = new Gson();
                                                        jDlianjiebean = gson.fromJson(response, JDlianjie.class);
                                                        datayouhui = jDlianjiebean.getData();
                                                        if (jDlianjiebean.getCode().equals("10000")) {
                                                            Intent intent = new Intent(getActivity(), JDred_packetActivity.class);
                                                            intent.putExtra("shopid", jDcomjPlistlist.get(location).getId());
                                                            startActivity(intent);
                                                        }
                                                    }
                                                });

                                            } else {
                                                Map<String, String> map = new HashMap<>();
                                                map.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
                                                map.put("spuId", jDcomHotlist.get(location).getId());
                                                map.put("appid", AUniqueIdentifier.getDeviceId(getActivity()));

                                                OkHttpUtils.post().params(map).url(GETCONURLURL).build().execute(new StringCallback() {
                                                    @Override
                                                    public void onError(Request request, Exception e) {

                                                    }

                                                    @Override
                                                    public void onResponse(String response) {
                                                        Log.e("优惠二和一数据", response);
                                                        Gson gson = new Gson();
                                                        jDlianjiebean = gson.fromJson(response, JDlianjie.class);
                                                        datayouhui = jDlianjiebean.getData();
                                                        if (jDlianjiebean.getCode().equals("10000")) {
                                                            if (jDlianjiebean.getData() != null) {
                                                                if(KeplerApiManager.getWebViewService().isKeplerLogined()){
                                                                    try {
                                                                        KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                                    } catch (KeplerBufferOverflowException e) {
                                                                        e.printStackTrace();
                                                                    }

                                                                }else {
                                                                    final LoginListener mLoginListener = new LoginListener() {
                                                                        @Override
                                                                        public void authSuccess(Object token) {
                                                                            // TODO Auto-generated method stub
                                                                            Toast.makeText(getActivity(), token + "", Toast.LENGTH_SHORT).show();
                                                                            //                showToken();

                                                                            if (KeplerApiManager.getWebViewService().isKeplerLogined()) {
                                                                                try {
                                                                                    KeplerApiManager.getWebViewService().openJDUrlWebViewPage(jDlianjiebean.getData() + "&userid" + SPUtils.getInstance().getString(SPConstants.KEY_USERID), "kepler_customer_Info");
                                                                                } catch (KeplerBufferOverflowException e) {
                                                                                    e.printStackTrace();
                                                                                }
                                                                            }

                                                                        }

                                                                        @Override
                                                                        public void authFailed(int errorCode) {
                                                                            // TODO Auto-generated method stub
                                                                            switch (errorCode) {
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_Init:// 初始化失败 break;
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_InitIng:// 初始化没有完成 break;
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_openH5authPageURLSettingNull://跳转url // 为null break;
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_getTokenErr:// 获取失败(OAuth授权之后，获取cookie过程出错) break;
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_User_Cancel:// 用户取消 break;
                                                                                case KeplerApiManager.KeplerApiManagerLoginErr_AuthErr_ActivityOpen://打开授权页面失败 break;
                                                                                default:
                                                                                    break;
                                                                            }
//                Toast.makeText(AllOrderActivity.this, errorCode + ":authFailed", Toast.LENGTH_SHORT).show();
                                                                            Log.e("jdSHOUQUAN", errorCode + ":authFailed");
                                                                        }
                                                                    };
                                                                    KeplerApiManager.getWebViewService().login(getActivity(), mLoginListener);
                                                                }
//                                                                Intent intent = new Intent(getActivity(), HtmlActivity.class);
//                                                                intent.putExtra("shopid", jDcomHotlist.get(location).getId());
//                                                                intent.putExtra("data", datayouhui);
//                                                                startActivity(intent);

                                                            }
                                                        }
                                                    }
                                                });


                                            }
                                        }


                                    }
                                }
                            }
                        });
                    }else{
                        Toast.makeText(getActivity(),jDcomHotlistBean.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }catch (Exception e){

        }
    }

}
